define(["jqform","layuiall","jq_request","zTree","baidueditor","helper"], function (jqform,layui,jq_request,zTree,UE,helper) {
    var form  = layui.form;
    var Controller = {
        listing:function () {
            jqform.delete();
            form.on('checkbox(select-all)', function(data){

                if(data.elem.checked==true){
                    $("input[name='idList[]']").prop('checked',true)
                }else {
                    $("input[name='idList[]']").prop('checked',false)
                }
                form.render('checkbox');
            });
        },
        addpost: function () {
            level({dataType: 'tree'});
            logic();
            jqform.init()
        },
        editpost:function () {
            level({dataType: 'tree'});
            logic();
            jqform.init()
        }
    }
    //层级选择
    function level(datas) {
        $(".menuLevel").click(function () {
            var TreeHtml = '<ul id="MenuTree" class="ztree"></ul>';
            var elm = $(this);
            var url = elm.attr("data-url");
            var role_id = [];
            var categoryList = [];
            var menuTreeAler = layer.open({
                type: 1,
                title: "分类选择",
                content: TreeHtml,
                area:["350px","500px"],
                shadeClose:true,
                btn:["确定"],
                success: function () {
                    $.ajax({
                        type:"get",
                        url :url,
                        dataType:'json',
                        data: datas,
                        success:function (res) {
                            var zTreeObj;
                            var setting = {
                                view: {
                                    showIcon: true
                                },
                                data: {
                                    simpleData: {
                                        enable: true,
                                        idKey:"id",
                                        pIdKey:"parent_id",
                                    },
                                    key:{
                                        name:"name"
                                    }
                                },
                                check: {
                                    enable: true,
                                    chkStyle: "checkbox",
                                    radioType: "all"
                                },
                                callback: {
                                    onClick: function (e, treeId, treeNode, clickFlag) {
                                        zTreeObj.checkNode(treeNode, !treeNode.checked, true);
                                        role_id = [];
                                        categoryList=[]
                                        nodes = zTreeObj.getCheckedNodes(true)
                                        for (var i = 0; i < nodes.length; i++) {
                                            role_id.push(nodes[i].id)
                                            categoryList.push(nodes[i].name)
                                        }
                                    },
                                    onCheck:function zTreeOnCheck(event, treeId, treeNode) {
                                        role_id = [];
                                        categoryList=[]
                                        nodes = zTreeObj.getCheckedNodes(true)
                                        for (var i = 0; i < nodes.length; i++) {
                                            role_id.push(nodes[i].id)
                                            categoryList.push(nodes[i].name)
                                        }

                                    },
                                }
                            };
                            zTreeObj = $.fn.zTree.init($("#MenuTree"), setting, res);
                            zTreeObj.expandAll(true);
                        }
                    })
                },
                yes:function () {
                    $("input[name='category_id']").val(role_id.join(","))
                    elm.val(categoryList.join(","))
                    layer.close(menuTreeAler)
                }
            })
        })
    }
    function logic() {
        UE.getEditor('container',{initialFrameWidth: "100%" });
        var laydate = layui.laydate;
        laydate.render({
            elem: '#publishedTime',
            type: 'datetime'
        });

        $(".app-photos .del-photos-btn").click(function () {
            $(this).parents("tr").eq($(this).index()).remove()
        });

        $(".attachment .del-photos-btn").click(function () {
            $(this).parents("tr").eq($(this).index()).remove()
        });

        $(".audios .del-photos-btn").click(function () {
            $(this).parents("tr").eq($(this).index()).remove()
        });

        $(".videos .del-photos-btn").click(function () {
            $(this).parents("tr").eq($(this).index()).remove()
        });

        $(".thumbnail .del-photos-btn").click(function () {
            $(this).parents("tr").eq($(this).index()).remove()
        });
        //相册上传
        /* uploadFile({
         elm:'#test1',
         url:"/admin/webupload/postupload",
         table:'.app-photos',
         multiple:true,
         exts:'jpg|png|bmp|jpeg|avg',
         inputName:'more[photos][]'
         });*/
        $("#test1").click(function () {
            uploadFiles({
                url: '/admin/webupload/webuploader/multi/1/filetype/image/app/admin/tab/local',
                file:function (res) {
                    var file = JSON.parse(res);
                    var str = '<tr>'+
                        '<td>'+
                        '<input type="text" class="layui-input" name="more[photos][]" value="'+file[0].filepath+'">'+
                        '</td>'+
                        '<td>'+
                        '<img width="80"  src="'+file[0].preview_url+'">'+
                        '</td>'+
                        '<td>'+
                        '<a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs del-photos-btn">删除</a>'+
                        '</td>'+
                        '</tr>';
                    $('.app-photos').append(str);
                    $(".del-photos-btn").click(function () {
                        $(this).parents("tr").eq($(this).index()).remove()
                    });
                    localStorage.removeItem("upload");
                }
            });
        });
        //附件件上传
        /*uploadFile({
         elm:'#test2',
         url:"/admin/webupload/postupload",
         table:'.attachment',
         multiple:true,
         exts:'jpg|png|bmp|jpeg|avg|txt|doc|docx|ico|zip|gzp',
         inputName:'more[attachments][]'
         });*/
        $("#test2").click(function () {
            uploadFiles({
                url: '/admin/webupload/webuploader/multi/1/filetype/file/app/admin/tab/local',
                file:function (res) {
                    var file = JSON.parse(res);
                    var str = '<tr>'+
                        '<td>'+
                        '<input type="text" class="layui-input" name="more[attachments][]" value="'+file[0].filepath+'">'+
                        '</td>'+
                        '<td>'+
                        '<img width="80"  src="'+file[0].preview_url+'">'+
                        '</td>'+
                        '<td>'+
                        '<a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs del-photos-btn">删除</a>'+
                        '</td>'+
                        '</tr>';
                    $('.attachment').append(str);
                    $(".del-photos-btn").click(function () {
                        $(this).parents("tr").eq($(this).index()).remove()
                    });
                    localStorage.removeItem("upload");
                }
            });
        });
        //音频上传
        /*uploadFile({
         elm:'#test3',
         url:"/admin/webupload/postupload",
         table:'.audios',
         multiple:true,
         exts:'mp3|mp4',
         inputName:'more[audios][]'
         });*/
        $("#test3").click(function () {
            uploadFiles({
                url: '/admin/webupload/webuploader/multi/1/filetype/audio/app/admin/tab/local',
                file:function (res) {
                    var file = JSON.parse(res);
                    var str = '<tr>'+
                        '<td>'+
                        '<input type="text" class="layui-input" name="more[audios][]" value="'+file[0].filepath+'">'+
                        '</td>'+
                        '<td>'+
                        '<img width="80"  src="'+file[0].preview_url+'">'+
                        '</td>'+
                        '<td>'+
                        '<a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs del-photos-btn">删除</a>'+
                        '</td>'+
                        '</tr>';
                    $('.audios').append(str);
                    $(".del-photos-btn").click(function () {
                        $(this).parents("tr").eq($(this).index()).remove()
                    });
                    localStorage.removeItem("upload");
                }
            });
        });
        //视频件上传
        /*uploadFile({
         elm:'#test4',
         url:"/admin/webupload/postupload",
         table:'.videos',
         multiple:true,
         exts:'mp3|mp4',
         inputName:'more[videos][]'
         });*/
        $("#test4").click(function () {
            uploadFiles({
                url: '/admin/webupload/webuploader/multi/1/filetype/video/app/admin/tab/local',
                file:function (res) {
                    var file = JSON.parse(res);
                    var str = '<tr>'+
                        '<td>'+
                        '<input type="text" class="layui-input" name="more[videos][]" value="'+file[0].filepath+'">'+
                        '</td>'+
                        '<td>'+
                        '<img width="80"  src="'+file[0].preview_url+'">'+
                        '</td>'+
                        '<td>'+
                        '<a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs del-photos-btn">删除</a>'+
                        '</td>'+
                        '</tr>';
                    $('.videos').append(str);
                    $(".del-photos-btn").click(function () {
                        $(this).parents("tr").eq($(this).index()).remove()
                    });
                    localStorage.removeItem("upload");
                }
            });
        });
        //封面上传
        /* uploadFile({
         elm:'#test5',
         url:"/admin/webupload/postupload",
         table:'.thumbnail',
         multiple:false,
         exts:'jpg|png|bmp|jpeg|avg',
         inputName:'thumbnail'
         });*/
        $("#test5").click(function () {
            uploadFiles({
                url: '/admin/webupload/webuploader/multi/1/filetype/image/app/admin/tab/local',
                file:function (res) {
                    var file = JSON.parse(res);
                    var str = '<tr>'+
                        '<td>'+
                        '<input type="text" class="layui-input" name="thumbnail" value="'+file[0].filepath+'">'+
                        '</td>'+
                        '<td>'+
                        '<img width="80"  src="'+file[0].preview_url+'">'+
                        '</td>'+
                        '<td>'+
                        '<a href="javascript:;" class="layui-btn layui-btn-danger layui-btn-xs del-photos-btn">删除</a>'+
                        '</td>'+
                        '</tr>';
                    $('.thumbnail').append(str);
                    $(".del-photos-btn").click(function () {
                        $(this).parents("tr").eq($(this).index()).remove()
                    });
                    localStorage.removeItem("upload");
                }
            });
        });
    }
    return Controller;
});